package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.handler.MessageContext;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private   UserService userService;
    @RequestMapping("/findUsername")
    public Result findUsername(){
        try {
            // 1）使用 帮助类(SecurityContextHolder)直接获取上下文对象(SecurityContext)
            SecurityContext securityContext = SecurityContextHolder.getContext();
            //2) 从上下文中获取用户信息
            //先获取认证对象
            Authentication authentication = securityContext.getAuthentication();
            //从认证对象中获取用户信息  (Principal : 重要的)
            Object principal = authentication.getPrincipal();
            //强转成User对象
            User user = (User) principal;
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }
    @RequestMapping("/findByPage")
     public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list =  userService.findAll();
     return  new Result(true,"查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,"查询失败");
        }
    }
    @RequestMapping("/add")
    public Result add(Integer[] roles, @RequestBody SysUser sysUser){
        try {
            String password = sysUser.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(password);
            sysUser.setPassword(encode);
            userService.add(roles,sysUser);
            return new Result(true, "添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加用户失败");
        }
    }
    @RequestMapping("/edit")
    public Result edit(Integer[] roles,@RequestBody SysUser sysUser){
        try {
            userService.edit(roles,sysUser);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            SysUser sysUser  = userService.findById(id);

            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS, sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/findRoleIdsBySykUserId")
    public Result findRoleIdsBySykUserId(Integer id){
        try {
            List<Integer> roles = userService.findRoleIdsBySykUserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/delById")
    public Result delById(Integer id){
        System.out.println("=========="+id);
        try {
            userService.delById(id);
            return new Result(true,"删除用户成功");
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
}
